package com.lee.dutylower.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.lee.dutylower.BaseActivity;
import com.lee.dutylower.MyApplication;
import com.lee.dutylower.R;
import com.lee.dutylower.db.greendao.SentryDao;
import com.lee.dutylower.db.greendao.SoldierDao;
import com.lee.dutylower.db.greendao.SquadDao;
import com.lee.dutylower.utils.FileUtils;
import com.lee.dutylower.utils.GreenDaoUtils;
import com.lee.dutylower.utils.SharedPreferencesUtils;

import java.io.File;


public class MainActivity extends BaseActivity {
    private TextView tvSquad;
    private TextView tvSoldier;
    private TextView tvSentry;
    private SquadDao squadDao;
    private SoldierDao soldierDao;
    private SentryDao sentryDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if("yes".equals(SharedPreferencesUtils.getParam(this,"isFirst","yes"))){
            GreenDaoUtils.initDB();
            SharedPreferencesUtils.setParam(this,"isFirst","no");
        }
        initView();
    }

    public void initView(){
        tvSquad = (TextView) findViewById(R.id.tv_class_value);
        tvSoldier = (TextView) findViewById(R.id.tv_soldier_value);
        tvSentry = (TextView) findViewById(R.id.tv_sentry_value);
        int sentrysize;
        int soldiersize;
        int squadsize;
        squadDao = MyApplication.getApplication().getDaoSession().getSquadDao();
        soldierDao = MyApplication.getApplication().getDaoSession().getSoldierDao();
        sentryDao = MyApplication.getApplication().getDaoSession().getSentryDao();
        if (squadDao.queryBuilder().build().list() != null && sentryDao.queryBuilder().build().list()!= null && soldierDao.queryBuilder().build().list() != null){
            squadsize = squadDao.queryBuilder().build().list().size();
            soldiersize = soldierDao.queryBuilder().build().list().size();
            sentrysize = sentryDao.queryBuilder().build().list().size();
        }else {
            squadsize = 0;
            soldiersize = 0;
            sentrysize = 0;
        }
        tvSquad.setText(" "+squadsize);
        tvSoldier.setText(" "+soldiersize);
        tvSentry.setText(" "+sentrysize);
    }

    public void toScheduling(View view) {
        FileUtils.dbcopy();
        startActivity(new Intent(this, SchedulingActivity.class));
    }

    public void toSetting(View view) {
        startActivity(new Intent(this, SettingActivity.class));
    }

    public void copy(View view) {
        File in = new File(Environment.getExternalStorageDirectory(),"duty-green.db");
        if (in.exists()){
            FileUtils.copyToDB(this);
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("数据加载")
                    .setMessage("备份数据成功导入，请退出软件重新进入！")
                    .setCancelable(false)
                    .setNegativeButton("关闭",null)
                    .create();
            dialog.show();
        }else {
            AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("加载数据！")
                    .setMessage("未检测到备份数据库！！！")
                    .setCancelable(false)
                    .setNegativeButton("关闭",null)
                    .create();
            dialog.show();
        }
    }

    public void backup(View view) {
        FileUtils.dbcopy();
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("数据备份成功")
                .setMessage("数据备份成功")
                .setCancelable(false)
                .setNegativeButton("关闭",null)
                .create();
        dialog.show();
    }
}
